// pages/wuliurongqiguanli/nav_check_gzcbl/check_gzcbl.js
const mybehavior = require("../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    use_in: "",
    value2: "",
    value3: "",
    search_value: "",
    search_value11: "",
    options1: [],
    options11: [],
    options12: [],
    options2: [],
    options3: ["来料包装标准",],
    popup_show1: false,
    popup_show2: false,
    popup_show3: false,
    baseUrl: "https://logisticsengineering.gree.com/pfep2_api",
    list: [],
    radio: "",
    material_code: "",
    batch_num: "",
    material_code_car: "",
    radio_show: true,
    data_list: [],
    key_batch_num: "",
    zz_show: false
  },
  onInput(e) {
    let key = e.currentTarget.dataset.key
    let arr = ["material_code", "batch_num", "material_code_car"]
    for (let i in arr) {
      if (arr[i] != key) {
        this.setData({
          [arr[i]]: ""
        })
      } else {
        this.setData({
          key_batch_num: e.detail

        })

      }
    }

  },
  onRadioChange() {


    let url = this.data.baseUrl + "/standard_doc_store_manage/transfer_batch2material_num"
    // "/standard_doc_store_manage/transfer_batch2material_num2"

    let data = {
      batch_num: this.data.batch_num
    }
    this.post(url, data).then(res => {
      console.log("批次号转物料编码", res.data)

      if (res.code == 20000) {
        setTimeout(() => {
          wx.hideLoading()
          this.setData({
            material_code: res.data.PARTCODE
          })
          this.getStandardDatas()

        }, 500)


      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })


  },
  onSearch1(e) {
    console.log(e.detail);

    if (e.detail == "") {
      let arr = this.data.options11
      this.setData({
        options1: arr
      })
    } else {
      let list1 = this.data.options11
      let arr = []
      for (let i = 0; i < list1.length; i++) {
        if (list1[i].match(this.data.search_value) != null) {
          arr.push(list1[i]);
        }
      }
      this.setData({
        options1: arr
      })
    }
  },

  // 获取--供应商--数据
  getPermissionDatas() {
    let url = this.data.baseUrl + "/data_auth_manage/get_use_in"
    let data = {

    }
    this.post(url, data).then(res => {
      // console.log("获取供应商数据", res.data)
      let data_list = res.data
      // console.log("xxx", data_list);

      if (res.code == 20000) {

        this.setData({
          options1: data_list,
          options2: data_list,
          options11: data_list,
          options12: data_list,
          previewImageClick: true

        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 点击公司名称选取输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择公司名称输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  // 确认公司名称值--弹出框关闭
  onChange1(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      use_in: value,
      popup_show1: false,
    })
  },


  // 扫描
  scan1(e) {
    this.setData({
      material_code: "",
      batch_num: "",
      material_code_car: "",
    })
    let key = e.currentTarget.dataset.key
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        // self.setData({
        //   [key]: label_code,
        // })

        self.getStandardDatas(label_code)

      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },

  // 获取--物料编码模板--数据
  getStandardDatas(e) {
    wx.showLoading({
      title: '处理中...',
    })
    let url = this.data.baseUrl + "/standard_doc_store_manage/transfer_batch2material_num2"
    let data = {
      batch_num: e,
      use_in: this.data.use_in,
    }
    console.log("传过去的数据", data);
    this.post(url, data).then(res => {
      console.log("物料编码模板", res.data.results);

      if (res.code == 20000) {
        wx.hideLoading()
        if (res.data.material_type != "自制件") {
          if (res.data.str_type == "物料编码") {
            this.setData({
              material_code: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料标识卡") {
            this.setData({
              material_code_car: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料批次号") {
            this.setData({
              batch_num: res.data.raw_str,
            })
          }
          if (res.data.results.length == 0 && res.data.str_type != "" && res.data.material_num != "") {
            wx.showToast({
              title: '该物料编码无标准',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料批次号" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料批次号无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料标识卡" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料标识卡无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "" && res.data.material_num == "") {
            wx.showToast({
              title: '该编码不存在',
              icon: 'none'
            })
          }

          this.setData({
            zz_show: false,
            data_list: res.data.results,
            material_code: res.data.material_num
          })
        } else {
          wx.hideLoading()
          this.setData({
            zz_show: true,
            data_list: res.data.results

          })

        }
      } else {
        wx.hideLoading()
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      wx.hideLoading()
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 按钮查询获取--物料编码模板--数据
  getStandardDatas1() {
    let url = this.data.baseUrl + "/standard_doc_store_manage/transfer_batch2material_num2"
    let data = {
      batch_num: this.data.key_batch_num,
      use_in: this.data.use_in,
    }
    console.log("传过去的数据", data);
    this.post(url, data).then(res => {
      console.log("物料编码模板", res.data.results);

      if (res.code == 20000) {
        if (res.data.material_type != "自制件") {
          if (res.data.str_type == "物料编码") {
            this.setData({
              material_code: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料标识卡") {
            this.setData({
              material_code_car: res.data.raw_str,
            })
          } else if (res.data.str_type == "物料批次号") {
            this.setData({
              batch_num: res.data.raw_str,
            })
          }
          if (res.data.results.length == 0 && res.data.str_type != "" && res.data.material_num != "") {
            wx.showToast({
              title: '该物料编码无标准',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料批次号" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料批次号无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "物料标识卡" && res.data.material_num == "") {
            wx.showToast({
              title: '该物料标识卡无物料编码信息',
              icon: 'none'
            })
          }
          if (res.data.results.length == 0 && res.data.str_type == "" && res.data.material_num == "") {
            wx.showToast({
              title: '该编码不存在',
              icon: 'none'
            })
          }

          this.setData({
            zz_show: false,
            data_list: res.data.results,
            material_code: res.data.material_num
          })
        } else {
          this.setData({
            zz_show: true,
            data_list: res.data.results

          })
          console.log("自制件", res.data);
        }



      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  showDetail(e) {
    let row = e.currentTarget.dataset.row_data
    console.log(row);

    wx.navigateTo({
      url: '/packageB/pages/package/pfep/standard_query/supplier_modify_standard/supplier_modify_standard',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', row)

      }
    })
  },
  showDetail1(e) {
    let row = e.currentTarget.dataset.row_data
    console.log(row);

    wx.navigateTo({
      url: '/packageB/pages/package/pfep/standard_query/zzj_standard/zzj_standard',
      success(res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', row)

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let gs = wx.getStorageSync('company')
    this.setData({
      use_in: gs
    })
    this.getPermissionDatas()


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})